﻿using System;
using System.IO;
using System.Text;
using System.Windows.Controls;

namespace DocxHelper
{
    public class TextBoxWriter : TextWriter
    {
        private readonly TextBox _textBox;

        public TextBoxWriter(TextBox output)
        {
            _textBox = output;
        }

        public override void Write(char value)
        {
            _textBox.Dispatcher.BeginInvoke(new Action(() =>
            {
                _textBox.AppendText(value.ToString());
            }));
        }

        public override void Write(char[] buffer, int index, int count)
        {
            var time = $"{DateTime.Now:HH:mm:ss}\t".ToCharArray();
            base.Write(time, 0, time.Length);
            base.Write(buffer, index, count);
            _textBox.Dispatcher.BeginInvoke(new Action(() =>
            {
                _textBox.ScrollToEnd();
            }));
        }

        public override Encoding Encoding => Encoding.UTF8;
    }
}